package com.mryao.workQueue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class NewTask {
    public final static String TASK_QUEUE_NAME = "Task.test";

    public static void main (String[] args) throws IOException, TimeoutException {
        //获取工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置ip
        factory.setHost("192.168.231.132");
        factory.setUsername("admin");
        factory.setPassword("admin");
        //打开连接
        Connection connection = factory.newConnection();
        //打开通道
        Channel channel = connection.createChannel();
        //声明队列
        //b:是否持久化 b2:是否独占队列 b3:当所有消费者客户端连接断开时是否自动删除队列 map:其他参数
        channel.queueDeclare(TASK_QUEUE_NAME,false,false,false,null);
        for(int i = 0 ; i < 10 ;i++) {
            //向队列发送消息
            String message = "hello ....." + i;
            channel.basicPublish("", TASK_QUEUE_NAME, null, message.getBytes("UTF-8"));
            System.out.println("[X  ] send '" + message + "'");
        }
        //释放资源
        channel.close();
        connection.close();
    }

    private static String getMessage(String[] strings){
        if(strings.length < 1){
            return "hello wrold";
        }
        return joinStrings(strings, " ");
    }

    private static String joinStrings(String[] strings, String delimiter){
        int length = strings.length;
        if(length == 0)
            return "";
        StringBuffer words = new StringBuffer(strings[0]);
        for(int i = 0 ; i < length ; i++ ){
            words.append(delimiter).append(strings[i]);
        }
        return words.toString();
    }

}
